#region Usings

using System;
using System.IO;
using Castle.ActiveRecord;
using NUnit.Framework;
using Karma.Framework.Core.Domain.Tools.Import.Data;
using Karma.Framework.Core.Domain.Tools.Import.Layout;
using Karma.Framework.Core.Domain.Tools.Import.Validation;
using Karma.Framework.Core.Test.Domain.Tools.Importador.Entidades;
using Karma.Framework.Core.UnitTest;

#endregion

namespace Karma.Framework.Core.Test.Domain.Tools.Importador
{
    [TestFixture]
    public class EntityPersisterFixture : AbstractTestCase
    {
        [Test]
        public void GuardarYActualizarEntidadSimpleTest()
        {
            ActiveRecordMediator<EntidadSimple>.Save(new EntidadSimple
                                                         {
                                                             Cadena = "zzzz",
                                                             Entero = 100,
                                                             Fecha = DateTime.Now
                                                         });
            FlushAndRecreateScope();
            DisposeScope();

            var factory = new DefaultLayoutFactory();
            var layout = factory.Create<EntidadSimple>(new LayoutFactoryConfig(), new Type[] {});

            var archivoBuilder = new ExcelLayoutFileReader();
            var data = archivoBuilder.ReadFileData(layout, new FileStream(
                                                               "..\\..\\Domain\\Tools\\Importador\\Archivos\\" +
                                                               layout.Entity.Name + ".xlsx", FileMode.Open,
                                                               FileAccess.ReadWrite, FileShare.Read));

            var entityBuilder = new DefaultEntityBuilder<EntidadSimple>();
            var entities = entityBuilder.BuildEntities(data);

            var persister = new DefaultEntityPersister();
            persister.Persist(layout, entities);

            CreateScope();
            var count = ActiveRecordMediator.Count(typeof (EntidadSimple));

            Assert.AreEqual(5, count);
        }

        [Test]
        public void SoloActualizarEntidadSimpleTest()
        {
            ActiveRecordMediator<EntidadSimple>.Save(new EntidadSimple
                                                         {
                                                             Cadena = "aaaa",
                                                             Entero = 1,
                                                             Fecha = DateTime.Now
                                                         });
            ActiveRecordMediator<EntidadSimple>.Save(new EntidadSimple
                                                         {
                                                             Cadena = "bbbb",
                                                             Entero = 2,
                                                             Fecha = DateTime.Now
                                                         });
            FlushAndRecreateScope();
            DisposeScope();

            var factory = new DefaultLayoutFactory();
            var layout = factory.Create<EntidadSimple>(new LayoutFactoryConfig(), new Type[] {});

            var archivoBuilder = new ExcelLayoutFileReader();
            var data = archivoBuilder.ReadFileData(layout, new FileStream(
                                                               "..\\..\\Domain\\Tools\\Importador\\Archivos\\" +
                                                               layout.Entity.Name + "UnRegistro.xlsx", FileMode.Open,
                                                               FileAccess.ReadWrite, FileShare.Read));

            var entityBuilder = new DefaultEntityBuilder<EntidadSimple>();
            var entities = entityBuilder.BuildEntities(data);

            var persister = new DefaultEntityPersister();
            persister.Persist(layout, entities);

            CreateScope();
            var count = ActiveRecordMediator.Count(typeof (EntidadSimple));

            Assert.AreEqual(2, count);
        }

        [Test]
        public void SoloGuardarEntidadConAsociadosTest()
        {
            ActiveRecordMediator<EntidadSimple>.Save(new EntidadSimple
                                                         {
                                                             Cadena = "aaaa",
                                                             Entero = 1,
                                                             Fecha = DateTime.Now
                                                         });
            ActiveRecordMediator<EntidadSimple>.Save(new EntidadSimple
                                                         {
                                                             Cadena = "bbbb",
                                                             Entero = 2,
                                                             Fecha = DateTime.Now
                                                         });
            FlushAndRecreateScope();
            DisposeScope();

            // generate layout
            var factory = new DefaultLayoutFactory();
            var layout = factory.Create<EntidadConAsociados>(new LayoutFactoryConfig(), new Type[] {});

            // validate file layout
            var layoutValidator = new ExcelLayoutValidator();
            layoutValidator.Validate(layout, new FileStream(
                                                 "..\\..\\Domain\\Tools\\Importador\\Archivos\\" +
                                                 layout.Entity.Name + ".xlsx",
                                                 FileMode.Open,
                                                               FileAccess.ReadWrite, FileShare.Read));

            // read file data
            var archivoBuilder = new ExcelLayoutFileReader();
            var data = archivoBuilder.ReadFileData(layout, new FileStream(
                                                               "..\\..\\Domain\\Tools\\Importador\\Archivos\\" +
                                                               layout.Entity.Name + ".xlsx", FileMode.Open,
                                                               FileAccess.ReadWrite, FileShare.Read));

            // validate data format
            var dataValidator = new DefaultDataValidator();
            dataValidator.Validate(data);

            // build entities
            var entityBuilder = new DefaultEntityBuilder<EntidadConAsociados>();
            var entities = entityBuilder.BuildEntities(data);

            // validate references
            var referenceValidator = new DefaultReferenceValidator();
            referenceValidator.Validate(layout, entities);

            // persist entities
            var persister = new DefaultEntityPersister();
            persister.Persist(layout, entities);

            CreateScope();
            var count = ActiveRecordMediator.Count(typeof (EntidadConAsociados));

            Assert.AreEqual(entities.Count, count);
        }

        [Test]
        public void SoloGuardarEntidadSimpleTest()
        {
            DisposeScope();

            var factory = new DefaultLayoutFactory();
            var layout = factory.Create<EntidadSimple>(new LayoutFactoryConfig(), new Type[] {});

            var archivoBuilder = new ExcelLayoutFileReader();
            var data = archivoBuilder.ReadFileData(layout, new FileStream(
                                                               "..\\..\\Domain\\Tools\\Importador\\Archivos\\" +
                                                               layout.Entity.Name + ".xlsx", FileMode.Open,
                                                               FileAccess.ReadWrite, FileShare.Read));

            var entityBuilder = new DefaultEntityBuilder<EntidadSimple>();
            var entities = entityBuilder.BuildEntities(data);

            var persister = new DefaultEntityPersister();
            persister.Persist(layout, entities);

            CreateScope();
            var count = ActiveRecordMediator.Count(typeof (EntidadSimple));

            Assert.AreEqual(entities.Count, count);
        }
    }
}